package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.epfresh.api.constant.AppSupplyRouterConstant;
import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.widget.list.LoadMoreListView;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.BookBestAdapter;
import com.epfresh.supplier.entity.BookBestMore;
import com.epfresh.supplier.entity.BookInfoEntity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class BookBestMoreActivity extends BaseActivity implements LoadMoreListView.IXListViewListener{


    private TextView tv_time_now,tv_money_now;//,tv_title,tv_package,tv_number;
/*    ImageView iv_product;*/
    LoadMoreListView lv_deal_detail;
    BookBestAdapter bookBestAdapter;

    ArrayList<BookInfoEntity.ProductListBean> list = new ArrayList<>();

    String timeTitle,timeValue,productId,type;
    double money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_best_more);
        tvBack.setVisibility(View.VISIBLE);
        ivBack.setVisibility(View.VISIBLE);

        money = Double.parseDouble(getIntent().getStringExtra("money"));
        timeValue = getIntent().getStringExtra("timeValue");
        timeTitle = getIntent().getStringExtra("timeTitle");
        productId = getIntent().getStringExtra("productId");
        type = getIntent().getStringExtra("type");

        toolbarTitle.setText(timeTitle+"销售金额");

        View headView;
        headView = getLayoutInflater().inflate(R.layout.include_book_head_detail, null);

        headView.findViewById(R.id.ll_tab).setVisibility(View.GONE);
        tv_time_now = (TextView) headView.findViewById(R.id.tv_time_now);
        tv_money_now = (TextView) headView.findViewById(R.id.tv_money_now);
        tv_time_now.setText(timeTitle);
        tv_money_now.setText(money+"元");

        lv_deal_detail = (LoadMoreListView)findViewById(R.id.lv_deal_detail);
        lv_deal_detail.addHeaderView(headView);
        lv_deal_detail.setXListViewListener(this);
            bookBestAdapter = new BookBestAdapter(BookBestMoreActivity.this, list);//, money);
            lv_deal_detail.setAdapter(bookBestAdapter);
            bookBestAdapter.setOnItemClickListener(BookBestMoreActivity.this);
        mHttpLoad(0);
    }
    //当前分页
    private int currentPage = 0;
    @Override
    public void onLoadMore() {
        mHttpLoad(++currentPage);
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.ll_best_item:
                Object o = view.getTag(R.id.item_key_position);
                if (o != null && o instanceof Integer) {
                    int position = (int) o;
                    if(timeValue.length()==4) {
                        Intent detailIntent = new Intent(this,
                                BookGoodsYearActivity.class);
                        detailIntent.putExtra("timeTitle", timeTitle);
                        detailIntent.putExtra("timeValue", list.get(position).getQueryDate());
                        detailIntent.putExtra("money", "" + list.get(position).getPrice() + "元");
                        detailIntent.putExtra("productId", list.get(position).getSpecId());
                        startActivity(detailIntent);
                    }else{
                        Intent detailIntent = new Intent(this,
                                BookGoodsMonthActivity.class);
                        detailIntent.putExtra("timeTitle", timeTitle);
                        detailIntent.putExtra("timeValue", list.get(position).getQueryDate());
                        detailIntent.putExtra("money", "" + list.get(position).getPrice() + "元");
                        detailIntent.putExtra("productId", list.get(position).getSpecId());
                        startActivity(detailIntent);
                    }
                }
                break;
            case R.id.tv_more_best:

                break;
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        outState.putString("timeValue",timeValue);
        outState.putString("productId",productId);
        outState.putDouble("money",money);
        outState.putString("timeTitle",timeTitle);
        outState.putString("type",type);
        super.onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    /**
     * 加载收入数据
     */
    private void mHttpLoad(int page) {
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(AppSupplyRouterConstant.cmd_order_supplier_order_incomeProductMore);
        Map<String, String> map = new HashMap<>();
        map.put("storeId", BaseApplication.getInstance().getStoreId());

        map.put("queryDate", timeValue);
        map.put("accountId",getUser().getAccountId());
        map.put("productId", productId);
        map.put("pageSize", "20");
        map.put("pageNumber", ""+page);

        data.setParameters(map);
        request(data, AppSupplyRouterConstant.cmd_order_supplier_order_incomeProductMore, onRequestListener);
    }

    private OnRequestListener<BookBestMore> onRequestListener = new OnRequestListener<BookBestMore>() {
        @Override
        public BookBestMore jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, BookBestMore.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            finish();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            finish();
        }

        @Override
        public void onStart(Object tag) {
            if(currentPage==0)
              showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<BookBestMore> response, Object tag) {

            hideProgressDialog();
            BookBestMore responseElement = response.getResponseElement();

            //list.clear();
            list.addAll(responseElement.getList());
            currentPage = responseElement.getPageNumber();

            if (responseElement.isIsLastPage()){
                lv_deal_detail.noMore();
            }else {
                lv_deal_detail.stopLoadMore();
            }
            bookBestAdapter.notifyDataSetChanged();
        }
    };

}
